Credibility of Witnesses
Memory limit: 32 MB
Witnesses, numbered from
to
, have made their testimonies in a court.
Each testimony is a statement of the form: “the
-th witness agrees with the
-th witness”
or “the
-th witness does not agree with the
-th witness”.
If the
-th witness agrees with the
-th witness then he agrees also with all the witnesses that the
-th witness agrees with.
Similarly, the
-th witness does not agree with all the witnesses that the
-th witness does not agree with.
We assume that every witness implicitly states: “I agree with myself”.
We say that witness
is not credible if from testimonies made in a court it follows that there exists a witness
such that
agrees with
and
does not agree with
.
Task
Write a program that:
- reads the number of witness and their testimonies from the standard input,
- finds all the witnesses that are not credible,
- writes the result to the standard output.
Input
In the first line of the standard input there is a single integer
,
,
which is the number of witnesses.
In the second line there is a single integer
,
,
which is the number of testimonies.
In each of the following
lines there are two integers
(
,
),
separated by a single space.
If
is positive it describes a testimony: “the
-th witness agrees with the
-th witness”.
If
is negative it means: “the
-th witness does not agree with the
-th witness”.
Output
In the standard output there should be written:
- a single word NIKT (which means “nobody” in Polish),
if it follows from testimonies that there is no witness that is not credible,
- or — in increasing order — the numbers of witnesses that are not credible
(one member in one line).
Example
For the input data:
6
12
1 3
1 6
2 -1
3 4
4 1
4 -2
4 5
5 -1
5 -3
5 2
6 5
6 4
the correct result is:
1
3
4
6
Task author: Grzegorz Jakacki.